package com.example.renshiguanli.controller;

import com.example.renshiguanli.config.PaginationConstant;
import com.example.renshiguanli.mapper.DeptMapper;
import com.example.renshiguanli.mapper.EmpMapper;
import com.example.renshiguanli.mapper.NationMapper;
import com.example.renshiguanli.mapper.PositionMapper;
import com.example.renshiguanli.pojo.Dept;
import com.example.renshiguanli.pojo.Emp;
import com.example.renshiguanli.pojo.Nation;
import com.example.renshiguanli.pojo.Position;
import com.example.renshiguanli.service.EmpService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class EmpController {
    @Autowired
    private EmpService empService;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private PositionMapper positionMapper;
    @Autowired
    private NationMapper nationMapper;
    @Autowired
    private EmpMapper empMapper;
    @GetMapping("/emps")
    public String emps(Model model, @RequestParam(value="pageNum",defaultValue="1") Integer pageNum){
        if(ObjectUtils.isEmpty(pageNum)){
            pageNum= PaginationConstant.CURRENT_NUM;
        }
        //设置分页(当前页，和每页显示数据条数)
        PageHelper.startPage(pageNum, PaginationConstant.PAGE_SIZE);
        List<Emp> all = empMapper.getEmpAndDept();
        PageInfo<Emp> pageInfo=new PageInfo<>(all);
        //用Model传递对象到page页面
        model.addAttribute("pageInfo",pageInfo);
        return "emp/list";

    }
//    员工管理中的添加员工需要查询民族，所属部门和职位
//    返回员工添加界面，并展示民族，所属部门和职位信息
    @GetMapping("/emp")
    public String addByFind(Model model){
        List<Dept> allByDept = deptMapper.findAllByDept();
        model.addAttribute("depts",allByDept);
        List<Nation> allByNation = nationMapper.findAllByNation();
        model.addAttribute("nations",allByNation);
        List<Position> allByPosition = positionMapper.findAllByPosition();
        model.addAttribute("positions",allByPosition);
        return "emp/add";
    }
//    添加员工
    @PostMapping("/emp")
    public String addEmp(Emp emp){
        empMapper.add(emp);
        return "redirect:/emps";
    }
//    修改员工
//    展示修改界面，将用户信息在改界面显示出来
    @GetMapping("/emp/{id}")
    public String updateByFind(@PathVariable("id") Integer id, Model model){
        Emp emp = empMapper.findById(id);
        model.addAttribute("emp",emp);
//        展示部门信息，职位信息，民族信息
        List<Dept> allByDept = deptMapper.findAllByDept();
        model.addAttribute("depts",allByDept);
        List<Nation> allByNation = nationMapper.findAllByNation();
        model.addAttribute("nations",allByNation);
        List<Position> allByPosition = positionMapper.findAllByPosition();
        model.addAttribute("positions",allByPosition);
        return "emp/add";
    }
//    修改用户信息
    @PutMapping("/emp")
    public String updateById(Emp emp){
        empMapper.update(emp);
        return "redirect:/emps";
    }
//    删除用户
//    根据id删除
    @DeleteMapping("/emp/{id}")
    public String deleteById(@PathVariable("id") Integer id){
        empMapper.delete(id);
        return "redirect:/emps";
    }
//    员工搜索Search
    @PostMapping("/search")//提交表单+传回前端的映射
    public String queryEmp(@RequestParam String name, Model model,@RequestParam(value="pageNum",defaultValue="1")Integer pageNum){
        PageHelper.startPage(pageNum, PaginationConstant.PAGE_SIZE);
        //查找数据
        List<Emp> emps=empService.findByName(name);
        model.addAttribute("emps",emps);//传回前端
        PageInfo<Emp> pageInfo=new PageInfo<>(emps);
        // model.addAttribute("emps",emps);
        //用Model传递对象到page页面
        model.addAttribute("pageInfo",pageInfo);
        return "emp/list";
    }
}
